package com.e.dianshang.controller.Api;

import com.e.dianshang.service.AfterSellService;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

@RestController
@CrossOrigin
@Api(value = "用户售后模块",tags = "售后api")
public class AfterSellApi {

    @Resource
    AfterSellService afterSellService;

    JsonResult jsonResult;

    @RequestMapping("insertAfterSell")
    @ApiOperation(value = "添加售后订单信息",httpMethod = "POST")
    @ApiImplicitParam(name = "map",value = "售后表的信息")
    public Map insertSell(@RequestBody Map map) throws ParseException {
        jsonResult=new JsonResult();
        afterSellService.updatePayType(map);
        map.remove("state");
        String date = String.valueOf(new Date());
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat formate2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        Date parse = formate2.parse(date);
        Timestamp startTime = Timestamp.valueOf(dateFormat.format(parse));
        map.put("applydate",startTime);
        Integer integer = afterSellService.updateAfterSell(map);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("申请成功");
        }else {
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }

    @RequestMapping("updateState")
    @ApiOperation(value = "修改订单的支付状态",httpMethod = "POST")
    @ApiImplicitParams(@ApiImplicitParam(name = "payType",value = "订单支付状态0fales,1true",dataType = "boolean"))
    public Map update(@RequestBody Map map){
        jsonResult=new JsonResult();
        Integer integer = afterSellService.updatePayType1(map);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else{
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }
}